function [ B ] = make_move( A, r, c, color )
%%MAKE_MOVE Matrix A is the current game board. r (row) and c (column) give 
%the position of the new stone. color gives the color of the player.
%   Detailed explanation goes here

%******************************   
    %linearise board matrix
    A = A(:);
 %******************************   
 
	% linear index of board matrix 
    index = r + (c-1)*8;

    %search in all directions    
    turn_chips(-9, min(r,c)-1); %left up
    turn_chips(-8, c-1); %left
    turn_chips(-7, min(8-r,c-1)); %left down
    turn_chips(-1, r-1); %up
    turn_chips(1, 8-r); %down
    turn_chips(7, min(r-1,8-c)); %right up
    turn_chips(8, 8-c); %right
    turn_chips(9, 8-max(r,c)); %right down
    
   
    %B = A; %if linearized version works, use this!!!
 %******************************   
    B = zeros(8);
    %return new matrix
    B(:,1) = A(1:8);
    B(:,2) = A(9:16);
    B(:,3) = A(17:24);
    B(:,4) = A(25:32);
    B(:,5) = A(33:40);
    B(:,6) = A(41:48);
    B(:,7) = A(49:56);
    B(:,8) = A(57:64);
 %*******************************   
    return;

    
    %*****************************************
    function turn_chips(direction, max)
        next = index + direction;
        
        for a = 1:1:max
            if(A(next)==0)
                break; %no chips to turn
            else
                if(A(next)==color)
                    A(index:direction:next) = color; %turn chips
                    break;
                end
            end
            next = next + direction; %next field
        end
    end
end

